﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Collections.Generic;

/// <summary>
/// 淘宝订单物流信息
/// </summary>
public class TB_Shipping
{
    public TB_Shipping()
    {      
    }
    /// <summary>
    /// 获取物流信息
    /// </summary>
    public int DownTBShipping(string username, string password, string appkey, string certcode, string sessionId)
    {
        int count = 0;
        int pageno = 1;
        while (true)
        {
            int ret = GetTBShipping(username, password, appkey, certcode, sessionId, pageno);
            pageno++;
            count += ret;
            if (ret == 0 || ret != 40)
                break;
        }
        return count;
    }
    public BS2.BSTaobaoAPI.BSTaobaoShipping GetTBShipping(string username, string password, string appkey, string certcode, string sessionId, string tid)
    {
        string[,] param = new string[,] {{ "sip_apiname", "taobao.shippings.send.get" },{"tid",tid},
                    {"fields","tid,seller_nick,buyer_nick,delivery_start, delivery_end,out_sid,receiver_name,created,modified,status,type,freight_payer"},
                    { "v", "1.0" }, { "sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "sip_format", "xml" },{"sip_appkey",appkey} };
        //BS2.BSTaobaoAPI.TaobaoAPI.setAPPID(appkey);
        //BS2.BSTaobaoAPI.TaobaoAPI.setCERTCODE(certcode);
        //BS2.BSTaobaoAPI.TaobaoAPI.setLoginPro(username, password);
        //BS2.BSTaobaoAPI.TaobaoAPI.setSessionID(sessionId);
        
        BS2.BSTaobaoAPI.BSTaobaoBussinessList c = BS2.BSTaobaoAPI.BSTaobaoBussinessAPI.getTaobaoShipping(param, appkey, certcode, sessionId);
        if (c.Count > 0)
        {
            BS2.BSTaobaoAPI.BSTaobaoShipping bs = (BS2.BSTaobaoAPI.BSTaobaoShipping)c[0];
            return bs;           
        }
        else
        {
            return null;
        }
        //XmlDocument xml = BS2.BSTaobaoAPI.TaobaoAPI.getXmlFromTaobao(param);
        //if (xml == null || xml.OuterXml.Contains("error_rsp"))
        //    return null;
        //List<BS2.BSTaobaoAPI.BSTaobaoShipping> shipList = GetShippingByXml(xml);
        //if (shipList != null && shipList.Count > 0)
        //    return shipList[0];
        //return null;
    }
    public int GetTBShipping(string username, string password, string appkey, string certcode, string sessionId, int page_no)
    {
        string[,] param = new string[,] {{ "sip_apiname", "taobao.shippings.send.get" },{"page_size","40"},{"page_no",page_no.ToString()},
                    {"fields","tid,seller_nick,buyer_nick,delivery_start, delivery_end,out_sid,receiver_name,created,modified,status,type,freight_payer"},
                    { "v", "1.0" }, { "sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "sip_format", "xml" },{"sip_appkey",appkey} };
        BS2.BSTaobaoAPI.TaobaoAPI.setAPPID(appkey);
        BS2.BSTaobaoAPI.TaobaoAPI.setCERTCODE(certcode);
        BS2.BSTaobaoAPI.TaobaoAPI.setLoginPro(username, password);
        BS2.BSTaobaoAPI.TaobaoAPI.setSessionID(sessionId);
        XmlDocument xml = BS2.BSTaobaoAPI.TaobaoAPI.getXmlFromTaobao(param);
        if (xml == null)
            return 0;
        List<BS2.BSTaobaoAPI.BSTaobaoShipping> shipList = GetShippingByXml(xml);
        int ret = 0;
        foreach (BS2.BSTaobaoAPI.BSTaobaoShipping myShip in shipList)
        {
            if (myShip == null)
                return 0;
            Model.TaoBao.TB_XSJRD_PS ps = new Model.TaoBao.TB_XSJRD_PS();
            ps.tid = myShip._tid;
            ps.buyer_nick = myShip._buyer_nick;
            ps.delivery_start = Convert.ToDateTime(myShip._delivery_start);
            ps.delivery_end = Convert.ToDateTime(myShip._delivery_end);
            ps.out_sid = myShip._out_sid;
            ps.receiver_name = myShip._receiver_name;
            ps.status = myShip._status;
            ps.type = myShip._type;
            ps.freight_payer = myShip._freight_payer;
            ps.seller_confirm = myShip._seller_confirm;
            ps.company_name = myShip._company_name;
            ps.created = Convert.ToDateTime(myShip._created);
            ps.modified = Convert.ToDateTime(myShip._modified);
            new BLL.TaoBao.TB_XSJRD_PS().Add(ps);
            ret++;
        }
        return ret;
    }
    List<BS2.BSTaobaoAPI.BSTaobaoShipping> GetShippingByXml(XmlDocument xml)
    {
        List<BS2.BSTaobaoAPI.BSTaobaoShipping> shipList = new List<BS2.BSTaobaoAPI.BSTaobaoShipping>();
        foreach (XmlNode node in xml.SelectNodes("/rsp/ship"))
        {
            BS2.BSTaobaoAPI.BSTaobaoShipping t = new BS2.BSTaobaoAPI.BSTaobaoShipping();
            if (node.SelectNodes("tid").Item(0) != null)
            {
                t._tid = node.SelectNodes("tid").Item(0).InnerText;
            }
            if (node.SelectNodes("buyer_nick").Item(0) != null)
            {
                t._buyer_nick = node.SelectNodes("buyer_nick").Item(0).InnerText;
            }
            if (node.SelectNodes("delivery_start").Item(0) != null)
            {
                t._delivery_start = node.SelectNodes("delivery_start").Item(0).InnerText;
            }
            if (node.SelectNodes("delivery_end").Item(0) != null)
            {
                t._delivery_end = node.SelectNodes("delivery_end").Item(0).InnerText;
            }
            if (node.SelectNodes("out_sid").Item(0) != null)
            {
                t._out_sid = node.SelectNodes("out_sid").Item(0).InnerText;
            }
            if (node.SelectNodes("item_title").Item(0) != null)
            {
                t._item_title = node.SelectNodes("item_title").Item(0).InnerText;
            }
            if (node.SelectNodes("receiver_name").Item(0) != null)
            {
                t._receiver_name = node.SelectNodes("receiver_name").Item(0).InnerText;
            }
            if (node.SelectNodes("status").Item(0) != null)
            {
                t._status = node.SelectNodes("status").Item(0).InnerText;
            }
            if (node.SelectNodes("type").Item(0) != null)
            {
                t._type = node.SelectNodes("type").Item(0).InnerText;
            }
            if (node.SelectNodes("freight_payer").Item(0) != null)
            {
                t._freight_payer = node.SelectNodes("freight_payer").Item(0).InnerText;
            }
            if (node.SelectNodes("seller_confirm").Item(0) != null)
            {
                t._seller_confirm = node.SelectNodes("seller_confirm").Item(0).InnerText;
            }
            if (node.SelectNodes("company_name").Item(0) != null)
            {
                t._company_name = node.SelectNodes("company_name").Item(0).InnerText;
            }
            if (node.SelectNodes("created").Item(0) != null)
            {
                t._created = node.SelectNodes("created").Item(0).InnerText;
            }
            if (node.SelectNodes("modified").Item(0) != null)
            {
                t._modified = node.SelectNodes("modified").Item(0).InnerText;
            }
            shipList.Add(t);
        }
        return shipList;
    }
}
